import {
  autoInputAccount, getDbList, deleteBox, zzsfpzhfwSB, zzsfpzhfwpdOption, asyncSetInterval, asyncTable, sbbdy, zzsybnsrPrint, fsjPrint, qsPrint, xgmPrint
} from './utils.js';

let pages = {
  loginIndex: 'https://etax.hubei.chinatax.gov.cn/portal/',
  home: 'https://etax.hubei.chinatax.gov.cn/portal/home.c',
  zzsfpzhfwpd: 'https://fpdk.hubei.chinatax.gov.cn/main.4a102dbd.html',
  zzsfpzhfwSB: 'https://etax.hubei.chinatax.gov.cn/zyy-typt/cssnj/http.action',
  sbbdy: 'https://etax.hubei.chinatax.gov.cn/portal/iframe.c',
  zzsybnsr: 'https://etax.hubei.chinatax.gov.cn/zyy-qtsx/bda0610606/printPDFall2019.action',
  fsjPrint: 'https://etax.hubei.chinatax.gov.cn/zyy-qtsx/bda0610678/printPDF.action',
  qsPrint: 'https://etax.hubei.chinatax.gov.cn/zyy-qtsx/bda0611033/printPDF2019.action',
  xgmPrint: 'https://etax.hubei.chinatax.gov.cn/zyy-qtsx/bda0610611/printPDF.action'
};
let zeroInfo;

const isPages = (page, isQuery = false) => {
  let href = window.location.href;
  if (isQuery) {
    return href.includes(pages[page]);
  }
  return href === pages[page];
};
const chooseCompany = async ({ nationalTaxNo }) => {
  return asyncSetInterval(()=>{
    let trs = $('#myModal_qrsf > div > div > div.modal-body > table > tbody tr');
    let isChoose = $('#frm_search > div > div.userInfobar > span');
    if (trs && trs.length && isChoose.length <= 1) {
      let currentCompany = Array.from(trs).find(item => $(item).find('td:nth-child(5)').text() === nationalTaxNo);
      $(currentCompany).find('td:nth-child(1) input').attr('checked', true);
      let nssf = document.getElementById('xznssf');
      nssf.submit();
      return true;
    }
    return true;
  }, 1000);
};

const startZeroReport = async ({ currentZeroInfo }) => {
  console.log('currentZeroInfo',currentZeroInfo);
  zeroInfo = currentZeroInfo;
  if (isPages('loginIndex')) {
    autoInputAccount(currentZeroInfo);
  }
  if (isPages('home')) {
    // 关闭弹窗
    await deleteBox($('.layui-layer.layui-layer-dialog > div.layui-layer-btn.layui-layer-btn- > a.layui-layer-btn0'));
    // 是否需要选择公司
    await chooseCompany(currentZeroInfo);
    let dblist = await getDbList(currentZeroInfo);
    if (dblist.length) {
      // 增值税（一般） 和 附加税
      let zzsAndfjs = $(dblist[0]).find('td:eq(2)').text() === '未申报';
      console.log('zzsAndfjs',zzsAndfjs)
      if (zzsAndfjs) {
        $(dblist[0]).find('td:nth-child(4) a').click();
      }
      setTimeout(function(){
        window.postMessage({
          event: 'closeTabs',
          data: ''
        }, '*');
      },1000)
    } else {
      asyncTableDetail();
    }
  }
  // if (isPages('zzsfpzhfwpd', true)) {
  //   zzsfpzhfwpdOption(currentZeroInfo);
  // }
  if (isPages('zzsfpzhfwSB', true)) {
    zzsfpzhfwSB(currentZeroInfo);
  }
  asyncTableDetailPage({ currentZeroInfo });
};

const asyncTableDetail = async () => {
  let lis = $('body > div.contenter > div > div.index-middle > div.index-left > div:nth-child(1) > ul > li');
  console.log(lis);
  let sbbdyBtn = Array.from(lis).find(item => $(item).text().includes('申报报表打印'));
  if (sbbdyBtn) sbbdyBtn.click();
};

// const baoBiaoDetailBack = async (data) => {
//   if (data.step === 1 && data.type === 'zzsYb') {
//     let dblist = await getDbList();
//     let zzsAndfjs = dblist.find(item => $($(item).find('td')[0]).text() === '增值税,地方教育附加,教育费附加,城市维护建设税' && $($(item).find('td')[2]).text() === '未申报');
//     $(zzsAndfjs).find('td a').click();
//   }
//   if (data.step === 2 && data.type === 'zzsYb') {
//     console.log(data);
//     console.log(zeroInfo);
//     let backInfo = {
//       ...zeroInfo,
//       recordList: [...data.list]
//     };
//     window.postMessage({
//       event: 'zeroReportBack',
//       data: backInfo
//     }, '*');
//   }
//   if (data.step === 1 && data.type === 'zzsYbDetail') {
//     sbbdy(zeroInfo, 'fjs');
//   }
//   if (data.step === 2 && data.type === 'fjsDetail') {
//     setTimeout(() => {
//       window.postMessage(
//         {
//           event: 'asyncTableDetailRequest'
//         },
//         '*'
//       );
//     }, 100);
//   }
// };

const asyncTableDetailPage = async ({ currentZeroInfo }) => {
  zeroInfo = currentZeroInfo;
  if (isPages('sbbdy', true)) {
    sbbdy(currentZeroInfo);
  }
  if (isPages('zzsybnsr', true)) {
    setTimeout(() => {
      zzsybnsrPrint(currentZeroInfo);
    }, 1000);
  }
  if (isPages('fsjPrint', true)) {
    setTimeout(() => {
      fsjPrint(currentZeroInfo);
    }, 1000);
  }

  if (isPages('qsPrint', true)) {
    setTimeout(() => {
      qsPrint(currentZeroInfo);
    }, 1000);
  }

  if (isPages('xgmPrint', true)) {
    setTimeout(() => {
      xgmPrint(currentZeroInfo);
    }, 1000);
  }
};

export {
  startZeroReport,
  // baoBiaoDetailBack,
  asyncTableDetail,
  asyncTableDetailPage
};
